package com.smartpro.mis.modular.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.smartpro.mis.modular.business.neo4jEntity.*;
import com.smartpro.mis.modular.business.service.MovieService;
import com.smartpro.mis.modular.business.service.N4jUserService;
import com.smartpro.mis.modular.system.dao.BuyNotPayRelationRepository;
import com.smartpro.mis.modular.system.dao.DealFinishRelationRepository;
import com.smartpro.mis.modular.system.dao.N4jProductRepository;
import com.smartpro.mis.modular.system.dao.N4jUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



/**
 * @author Mengiy
 */
@RestController
@RequestMapping("/api/service/n4j/")
public class N4jUserProductController {

	private final N4jUserService n4jUserService;

	@Autowired
	private N4jUserRepository n4jUserRepository;

	@Autowired
	private N4jProductRepository n4jProductRepository;

	@Autowired
	private BuyNotPayRelationRepository buyNotPayRelationRepository;
	@Autowired
	private DealFinishRelationRepository dealFinishRelationRepository;

	public N4jUserProductController(N4jUserService n4jUserService) {
		this.n4jUserService = n4jUserService;
	}

    @GetMapping("/graph")
	public Map<String, Object> graph(@RequestParam(value = "limit",required = false) Integer limit) {

//		add();
		return n4jUserService.graph(limit == null ? 100 : limit);
	}



	public void add(){

		N4jUser n4jUser = new N4jUser(1);
		n4jUserRepository.save(n4jUser);

		N4jProduct n4jProduct = new N4jProduct(1);
		n4jProductRepository.save(n4jProduct);

		N4jProduct n4jProduct2 = new N4jProduct(2);
		n4jProductRepository.save(n4jProduct2);

		BuyNotPayRelationship buyNotPayRelationship = new BuyNotPayRelationship("2019-05-14 10:04:25",n4jUser,n4jProduct);
		this.buyNotPayRelationRepository.save(buyNotPayRelationship);


//		UserProductRelationship userProductRelationship = new UserProductRelationship(RelationshipEnum.BUY_NOT_PAY,"2019-05-14 10:04:25"
//				,n4jUser,n4jProduct);
		List<UserProductRelationship> userProductRelationshipList = new ArrayList<>();
//		userProductRelationshipList.add(userProductRelationship);


		DealFinishRelationship dealFinishRelationship = new DealFinishRelationship("2019-05-14 10:04:25",n4jUser,n4jProduct2);
		this.dealFinishRelationRepository.save(dealFinishRelationship);
		userProductRelationshipList.add(buyNotPayRelationship);
		userProductRelationshipList.add(dealFinishRelationship);
		n4jUser.setUserProductRelationship(userProductRelationshipList);
		n4jUserRepository.save(n4jUser);


		System.out.println("**********************************************");
		N4jProduct n4jProduct1 = this.n4jProductRepository.findByProductID(1);
		System.out.println(JSONObject.toJSONString(n4jProduct1));

		System.out.println("**********************************************");
		List<N4jUser> userList = (ArrayList)this.n4jUserRepository.graph(10);
		for( N4jUser u:userList){

			System.out.println(JSONObject.toJSONString(u));
		}

		System.out.println("**********************************************");
		List<N4jProduct> proList = (ArrayList)this.n4jProductRepository.graph(10);
		for( N4jProduct pro:proList){

			System.out.println(JSONObject.toJSONString(pro));
		}

	}
}
